package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a01_用栈实现队列
 * </p>
 *
 * @author flyduck
 * @since 2024-07-25
 */
public class a02_用栈实现队列复习2 {

    public static void main(String[] args) {
        //["MyQueue","push","push","push","push","pop","push","pop","pop","pop","pop"]
        MyQueue2 myQueue2 = new MyQueue2();
        myQueue2.push(1);
        myQueue2.push(2);
        myQueue2.push(3);
        myQueue2.push(4);
        myQueue2.pop();
        myQueue2.push(5);
        myQueue2.pop();
        myQueue2.pop();
        myQueue2.pop();
        myQueue2.pop();
    }

}

class MyQueue3 {

    private Stack<Integer> inStack;//123
    private Stack<Integer> outStack;//

    public MyQueue3() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    public void push(int x) {
        inStack.push(x);
    }

    public int pop() {
        if (!outStack.isEmpty()){
            return outStack.pop();
        }else {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
            return outStack.pop();
        }
    }

    public int peek() {
        if (!outStack.isEmpty()){
            return outStack.peek();
        }else {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
            return outStack.peek();
        }
    }

    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }
}
